(function() {
  'use strict';

  angular
    .module('hp')
    // .service('MyAuth', MyAuth)
    .run(runBlock);

  /** @ngInject */
  function runBlock($log,$rootScope,$state,MyAuth,$window) {

    $log.debug('runBlock end');

    var isNotAuthPage;
        $rootScope.loading = false;
        // window.addEventListener('load', function() {
        //     new FastClick(document.body);
        // }, false);

        // $rootScope.user = {'token':MyAuth.loggedIn()} || {};
        $rootScope.isLogin = MyAuth.loggedIn();

        $rootScope.$on('$stateChangeStart', function (event, toState, toParams, fromState, fromParams) {
            // 没登录当然跳到登录页
            isNotAuthPage = angular.element.inArray(toState.name, ['home','login', 'register', 'about','404','error']) === -1;

            if (isNotAuthPage && !MyAuth.loggedIn()) {
                $state.go('login');
                event.preventDefault();
            }
        });

        $rootScope.$on('event:auth-require',function(data){
            $window.sessionStorage.removeItem('token')
            // $rootScope.user = {};
            $rootScope.isLogin = false;
			$state.go('login');
        });

        $rootScope.$on('loading:start',function(data){
			$rootScope.loading = true;
			// console.log('loading..._');
        });

        $rootScope.$on('loading:finished',function(data){
			$rootScope.loading = false;
			// console.log('loading finished.');
        })
  }



})();
